package com.hrt.manage.stock.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.ProType;
import com.hrt.bean.ProductInfor;
import com.hrt.bean.ResourceType;
import com.hrt.bean.Stocks;
import com.hrt.manage.product.service.GiftInforService;
import com.hrt.manage.product.service.ProInforService;
import com.hrt.manage.product.service.ProtypeService;

@Controller
@RequestMapping("manage/productStock")
public class ProductStockController extends BaseController<ProductInfor> {

	@Autowired
	private ProInforService proInforService;

	@Autowired
	private GiftInforService giftInforService;

	@Autowired
	private ProtypeService protypeService;

	private List<ProType> proTypes;

	private List<Map<String, Object>> reTypes;

	private boolean isInit;

	private ProductInfor product;

	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		isInit = true;
		view.setViewName("manage/stocks/productList");

		reTypes = this.getReTypes();
		proTypes = this.getProTypes();
		List list = new ArrayList();

		setTotalCount(new Long(list.size()));
		this.setList(list);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		this.setPageCount(this.computePageCount(getTotalCount(), getPageSize()));
		page = this.getPage();

		view.addObject("pageResult", page);
		view.addObject("reTypes", reTypes);
		view.addObject("proTypes", getProTypes());
		view.addObject("isInit", isInit);
		return view;
	}

	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "retype", required = false) Integer retype,

			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		proTypes = this.getProTypes();
		reTypes = this.getReTypes();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);
		List resultList = new ArrayList();

		if (retype != null) {
			if (retype.equals(ResourceType.PRODUCT_RESOURCE)) {
				resultList = proInforService.queryByPage(pageIndex, pageSize);
				this.setTotalCount(proInforService.totalCount());
			}

			if (retype.equals(ResourceType.GIFT_RESOURCE)) {
				resultList = giftInforService.queryByPage(pageIndex, pageSize);
				this.setTotalCount(giftInforService.totalCount());
			}
		} else {
			// send error message...
		}
		setList(resultList);
		setTotalCount(new Long(resultList.size()));
		this.setPageCount(computePageCount(getTotalCount(), getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);

		resultMap.put("typeId", retype);
		resultMap.put("reTypes", reTypes);
		return resultMap;
	}

	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) String proId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		proTypes = this.getProTypes();
		if (proId != null && !"".equals(proId)) {
			
			System.out.println("showshowshowshowshow " + proId);
			product = this.proInforService.getProductByid(new Long(proId));
			if (product != null) {
				map = transBean2Map(product);
				for (Iterator<Map.Entry> itr = map.entrySet().iterator(); itr
						.hasNext();) {
					Map.Entry entry = itr.next();
					System.out.println(entry.getKey().toString());
					System.out.println(entry.getValue().toString());
				}
			}

		}
		return map;

	}

	/**
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getReTypes() {
		reTypes = new ArrayList();
		reTypes = ResourceType.getReType();
		return reTypes;
	}

	public void setReTypes(List<Map<String, Object>> reTypes) {
		this.reTypes = reTypes;
	}

	public List<ProType> getProTypes() {

		proTypes = protypeService.loadTypes();

		return proTypes;
	}

	public void setProTypes(List<ProType> proTypes) {
		this.proTypes = proTypes;
	}
}
